﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace YXShop.Web.admin.product
{
    public partial class product_batch_export : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("001009007", "对不起，您没有商品批量导出的权限");
                if (Request.Form["Option"] != null && !Request.Form["Option"].Trim().Equals(""))
                {
                    string Name = Request.Form["Name"].ToString();
                    string productCID = ChangeHope.WebPage.PageRequest.GetFormString("productCId");
                    string productBrandId = ChangeHope.WebPage.PageRequest.GetFormString("productBrandId");
                    string productNo = ChangeHope.WebPage.PageRequest.GetFormString("productNo");
                    string types = Request.Form["Option"].Trim();
                    if (types == "SearchProduct")
                    {
                        this.SearchProduct(productCID, productBrandId, Name, productNo);
                    }
                    Response.End();
                    return;
                }

                YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
                this.txtProductClass.Attributes.Add("readonly", "readonly");
                this.txtProductClass.Attributes.Add("onclick", "selectFile('Random_Productclass',new Array(" + this.hfcid.ClientID + "," + this.txtProductClass.ClientID + "),310,450,'" + sp.DummyPaht + "');");
                this.BindBrand();
                this.BindProduct();
            }
        }

        protected void BindBrand()
        {
            YXShop.BLL.Product.ProductBrand bll = new YXShop.BLL.Product.ProductBrand();
            List<YXShop.Model.Product.ProductBrand> brandList = bll.GetDTList("");
            if (brandList.Count > 0)
            {
                this.ddlProductBrand.Items.Add(new ListItem("请选择...", "0"));
                for (int i = 0; i < brandList.Count; i++)
                {
                    this.ddlProductBrand.Items.Add(new ListItem(brandList[i].Name, brandList[i].ID.ToString()));
                }
            }
        }

        protected void SearchProduct(string CID, string BrandId, string ProductName, string ProductNO)
        {
            YXShop.BLL.Product.ProductInfo bll = new YXShop.BLL.Product.ProductInfo();
            string cond = "";
            if (!string.IsNullOrEmpty(ProductName))
            {
                cond += " and pro_Name like '%" + ProductName + "%'";
            }
            if (!string.IsNullOrEmpty(ProductNO))
            {
                cond += " and pro_NO like '%" + ProductNO + "%'";
            }
            if (!string.IsNullOrEmpty(BrandId) && BrandId != "0")
            {
                cond += " and pro_BrandID=" + BrandId + "";
            }
            if (!string.IsNullOrEmpty(CID) && CID != "0")
            {
                cond += " and cid in (" + YXShop.TemplateAction.Common.CodStr(CID) + ")";
            }
            DataTable dt = bll.DTGetListWhere(cond);
            StringBuilder shtml = new StringBuilder();
            shtml.Append("<select size=\"14\" id=\"Seleted\" style=\"width: 98%\"  multiple=\"multiple\">");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    shtml.Append("<option value='" + dt.Rows[i]["pro_ID"].ToString() + "' title='" + dt.Rows[i]["pro_Name"].ToString() + "'>" + dt.Rows[i]["pro_Name"].ToString() + "</option>");
                }
            }
            shtml.Append("</select>");
            Response.Write(shtml.ToString());
        }

        protected void BindProduct()
        {
            StringBuilder shtml = new StringBuilder();
            string productId = ChangeHope.WebPage.PageRequest.GetQueryString("barchId");
            shtml.Append("<select name=\"Selects\" size=\"14\" multiple=\"multiple\" style=\"width: 98%\">");
            if (!string.IsNullOrEmpty(productId))
            {
                YXShop.BLL.Product.ProductInfo bll = new YXShop.BLL.Product.ProductInfo();
                DataTable dt = bll.GetPartData(productId);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        shtml.Append("<option value='" + dt.Rows[i]["pro_ID"].ToString() + "' title='" + dt.Rows[i]["pro_Name"].ToString() + "' selected>" + dt.Rows[i]["pro_Name"].ToString() + "</option>");
                    }
                }

            }
            shtml.Append("</select>");
            this.ltProductSelected.Text = shtml.ToString();
        }

        protected void butExport_Click(object sender, EventArgs e)
        {
            

            YXShop.ExcelLite.ExcelFile excel = new YXShop.ExcelLite.ExcelFile();
            #region
            YXShop.ExcelLite.ExcelWorksheet sheet = excel.Worksheets.Add("商品信息");

            sheet.Rows[0].Height = 25 * 20;
            sheet.Cells[0, 0].Value = "商品名称";
            sheet.Cells[0, 1].Value = "商品货号";
            sheet.Cells[0, 2].Value = "单位";
            sheet.Cells[0, 3].Value = "商品标签";
            sheet.Cells[0, 4].Value = "商品标示";
            sheet.Cells[0, 5].Value = "商品品牌";
            sheet.Cells[0, 6].Value = "市场售价";
            sheet.Cells[0, 7].Value = "商城售价";
            sheet.Cells[0, 8].Value = "商品成本价";
            sheet.Cells[0, 9].Value = "商品重量";
            sheet.Cells[0, 10].Value = "是否上架销售";
            sheet.Cells[0, 11].Value = "上架时间";
            sheet.Cells[0, 12].Value = "下架时间";
            sheet.Cells[0, 13].Value = "上架数量";
            sheet.Cells[0, 14].Value = "赠送积分";
            sheet.Cells[0, 15].Value = "库存";
            sheet.Cells[0, 16].Value = "报警数量";
            sheet.Cells[0, 17].Value = "积分购买额度";
            sheet.Cells[0, 18].Value = "商品原始图";
            sheet.Cells[0, 19].Value = "商品图片";
            sheet.Cells[0, 20].Value = "商品缩略图";
            sheet.Cells[0, 21].Value = "简单描述";
            sheet.Cells[0, 22].Value = "详细描述";
            sheet.Cells[0, 23].Value = "优化关键字";
            sheet.Cells[0, 24].Value = "优化标题";
            sheet.Cells[0, 25].Value = "优化描述";
            sheet.Cells[0, 26].Value = "内容页模板路径";
            sheet.Cells[0, 0].Style.HorizontalAlignment = YXShop.ExcelLite.HorizontalAlignmentStyle.Center;
            int Index = 0;
            while (Index <= 26)
            {
                sheet.Cells[0, Index].Style.Font.Size = 11 * 20;
                sheet.Cells[0, Index].Style.Font.Weight = 700;
                sheet.Cells[0, Index].Style.HorizontalAlignment = YXShop.ExcelLite.HorizontalAlignmentStyle.Center;
                sheet.Columns[Index].Style.Font.Name = "宋体";
                Index++;
            }
            sheet.Columns[0].Width = 20 * 256;
            sheet.Columns[1].Width = 10 * 256;
            sheet.Columns[2].Width = 15 * 256;
            sheet.Columns[3].Width = 15 * 256;
            sheet.Columns[4].Width = 15 * 256;
            sheet.Columns[5].Width = 15 * 256;
            sheet.Columns[6].Width = 15 * 256;
            sheet.Columns[7].Width = 10 * 256;
            sheet.Columns[8].Width = 20 * 256;
            sheet.Columns[9].Width = 8 * 256;
            sheet.Columns[10].Width = 15 * 256;
            sheet.Columns[11].Width = 20 * 256;
            sheet.Columns[12].Width = 15 * 256;
            sheet.Columns[13].Width = 20 * 256;
            sheet.Columns[14].Width = 15 * 256;
            sheet.Columns[15].Width = 20 * 256;
            sheet.Columns[16].Width = 15 * 256;
            sheet.Columns[17].Width = 20 * 256;
            sheet.Columns[18].Width = 15 * 256;
            sheet.Columns[19].Width = 20 * 256;
            sheet.Columns[20].Width = 20 * 256;
            sheet.Columns[21].Width = 20 * 256;
            sheet.Columns[22].Width = 20 * 256;
            sheet.Columns[23].Width = 20 * 256;
            sheet.Columns[24].Width = 20 * 256;
            sheet.Columns[25].Width = 20 * 256;
            sheet.Columns[26].Width = 20 * 256;
            #endregion
            Index = 1;
            string productId = ChangeHope.WebPage.PageRequest.GetFormString("Selects");
            YXShop.BLL.Product.ProductInfo bll = new YXShop.BLL.Product.ProductInfo();
            DataTable dt = bll.GetPartData(productId);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                #region
                sheet.Cells[Index, 0].Value = dt.Rows[i]["pro_Name"].ToString();
                sheet.Cells[Index, 1].Value = dt.Rows[i]["pro_NO"].ToString();
                sheet.Cells[Index, 2].Value = dt.Rows[i]["pro_Unit"].ToString();

                sheet.Cells[Index, 3].Value = dt.Rows[i]["pro_State"].ToString();
                sheet.Cells[Index, 4].Value = dt.Rows[i]["pro_Designation"].ToString();
                //sheet.Cells[Index, 4].Style.NumberFormat = "yyyy年MM月dd日";
                YXShop.BLL.Product.ProductBrand brandBll = new YXShop.BLL.Product.ProductBrand();
                YXShop.Model.Product.ProductBrand brandModel = new YXShop.Model.Product.ProductBrand();
                YXShop.Model.Product.ProductBrand getBrand = brandBll.GetModelID(Convert.ToInt32(dt.Rows[i]["pro_BrandID"].ToString()));
                if (getBrand != null)
                {
                    sheet.Cells[Index, 5].Value = brandModel.Name;
                }
                else
                {
                    sheet.Cells[Index, 5].Value = "";
                }
                
                //sheet.Cells[Index, 5].Style.NumberFormat = "yyyy年MM月dd日";
                sheet.Cells[Index, 6].Value =Convert.ToDouble(dt.Rows[i]["pro_MarketPrice"].ToString()).ToString("f2");
                sheet.Cells[Index, 7].Value = Convert.ToDouble(dt.Rows[i]["pro_ShopPrice"].ToString()).ToString("f2");
                sheet.Cells[Index, 8].Value = Convert.ToDouble(dt.Rows[i]["pro_CostPrice"].ToString()).ToString("f2");
                //sheet.Cells[Index, 8].Style.NumberFormat = "yyyy年MM月dd日";

                sheet.Cells[Index, 9].Value = dt.Rows[i]["pro_Weight"].ToString();
                sheet.Cells[Index, 10].Value = dt.Rows[i]["pro_Audit"].ToString();
                sheet.Cells[Index, 11].Value = dt.Rows[i]["pro_AutoUp"].ToString();
                sheet.Cells[Index, 12].Value = dt.Rows[i]["pro_AutoDown"].ToString();
                sheet.Cells[Index, 13].Value = dt.Rows[i]["pro_AuditCount"].ToString();
                sheet.Cells[Index, 14].Value = dt.Rows[i]["pro_DonateIntegral"].ToString();
                sheet.Cells[Index, 15].Value = dt.Rows[i]["pro_Stock"].ToString();
                sheet.Cells[Index, 16].Value = dt.Rows[i]["pro_Alarm"].ToString();
                sheet.Cells[Index, 17].Value = dt.Rows[i]["pro_IntegralPrice"].ToString();
                sheet.Cells[Index, 18].Value = dt.Rows[i]["pro_Original"].ToString();
                sheet.Cells[Index, 19].Value = dt.Rows[i]["pro_Images"].ToString();
                sheet.Cells[Index, 20].Value = dt.Rows[i]["pro_Thumbnail"].ToString();
                sheet.Cells[Index, 21].Value = dt.Rows[i]["pro_Synopsis"].ToString();
                sheet.Cells[Index, 22].Value = dt.Rows[i]["pro_Content"].ToString();
                sheet.Cells[Index, 23].Value = dt.Rows[i]["pro_MetaKeywords"].ToString();
                sheet.Cells[Index, 24].Value = dt.Rows[i]["pro_MetaTitle"].ToString();
                sheet.Cells[Index, 25].Value = dt.Rows[i]["pro_MetaDescription"].ToString();
                sheet.Cells[Index, 26].Value = dt.Rows[i]["pro_ContentTemplate"].ToString();
                Index++;
                #endregion
            }
            string FileName = Server.MapPath("~\\admin\\product\\file") + "\\ProductInfo_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
            excel.SaveXls(FileName);
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            Response.Redirect(sp.DummyPaht + "admin\\product\\file" + "\\ProductInfo_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
            File.Delete(FileName);
        }
    }
}
